package stack.era.domain;

import java.util.ArrayList;
import java.util.HashMap;

import stack.era.domain.location.Location;
import stack.era.domain.town.Town;
import stack.era.domain.world.World;
import stack.era.domain.character.Character;
import stack.era.domain.character.PlayerCharacter;

public class DomainHome {

	private static DomainHome instance = null;

	private ArrayList<String> registeredCharacters;
	private HashMap<String, Character> characters;

	private ArrayList<String> registeredTowns;
	private HashMap<String, Town> towns;

	private ArrayList<String> registeredLocations;
	private HashMap<String, Location> locations;

	private World world;

	private DomainHome() {
		registeredCharacters = new ArrayList<String>();
		characters = new HashMap<String, Character>();
		registeredTowns = new ArrayList<String>();
		towns = new HashMap<String, Town>();
		registeredLocations = new ArrayList<String>();
		locations = new HashMap<String, Location>();
		world = new World();
	}

	public synchronized static DomainHome getInstance() {
		if (instance == null) {
			instance = new DomainHome();
		}
		return instance;
	}

	public void registerCharacter(String name, Character character) {
		if (!registeredCharacters.contains(name)) {
			registeredCharacters.add(name);
			characters.put(name, character);
		}
	}

	public void registerTown(String name, Town town) {
		if (!registeredTowns.contains(name)) {
			registeredTowns.add(name);
			towns.put(name, town);
		}
	}

	public void registerLocation(String name, Location location) {
		if (!registeredLocations.contains(name)) {
			registeredLocations.add(name);
			locations.put(name, location);
		}
	}

	public Character getCharacter(String name) {
		return characters.get(name);
	}

	public Town getTown(String name) {
		return towns.get(name);
	}

	public Location getLocation(String name) {
		return locations.get(name);
	}

	public void setWorld(World world) {
		this.world = world;
	}

	public World getWorld() {
		return world;
	}

	public ArrayList<Character> getAllCharacters() {
		return new ArrayList<Character>(characters.values());
	}

	public ArrayList<Town> getAllTowns() {
		return new ArrayList<Town>(towns.values());
	}

	public ArrayList<Location> getAllLocations() {
		return new ArrayList<Location>(locations.values());
	}

	public PlayerCharacter getPlayerCharacter() {
		return (PlayerCharacter) characters.get(world.getPlayerCharacterName());
	}

}
